local sk__lingruo = fk.CreateSkill {

  name = "sk__lingruo",

  tags = {},

}



sk__lingruo:addEffect(fk.TargetSpecified, {
  name = "sk__lingruo",
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__lingruo.name) and
        (data.card.trueName == "slash" or data.card:isCommonTrick()) then
      return #data:getAllTargets() == 1 and data.to ~= player
    end
  end,
  on_cost = function(self, event, target, player, data)
    local dat = data.to
    if player.room:askForSkillInvoke(player, sk__lingruo.name, nil, "#sk__lingruo-invoke::" .. dat.id) then
      event:setCostData(self, { tos = { dat.id } })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self).tos[1])
    room:doIndicate(player.id, { to.id })
    local info1, info2 = {}, {}
    for _, id in ipairs(player:getCardIds("he")) do
      local type = Fk:getCardById(id):getTypeString()
      info1[type] = (info1[type] or 0) + 1
    end
    for _, id in ipairs(to:getCardIds("he")) do
      local type = Fk:getCardById(id):getTypeString()
      info2[type] = (info2[type] or 0) + 1
    end
    local n = 0
    for _, type in ipairs({ "basic", "trick", "equip" }) do
      info1[type] = info1[type] or 0
      info2[type] = info2[type] or 0
      if info1[type] > info2[type] then
        n = n + 1
      end
    end
    if n == 0 then return end
    for i = 1, n, 1 do
      local choices = {}
      if not player.dead then
        table.insert(choices, 1)
      end
      if not player.dead and not to.dead and not to:isNude() then
        table.insert(choices, 2)
      end
      if not to.dead and not to:isNude() then
        table.insert(choices, 3)
      end
      if #choices == 0 then return end
      local num = table.random(choices)
      if num == 1 then
        player:drawCards(1, sk__lingruo.name)
      else
        local card = table.random(to:getCardIds("he"))
        if num == 2 then
          room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, sk__lingruo.name, nil, false, player)
        else
          room:throwCard(card, sk__lingruo.name, to, to)
        end
      end
    end
  end,
})
sk__lingruo:addEffect(fk.TargetConfirmed, {
  name = "sk__lingruo",
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__lingruo.name) and
        (data.card.trueName == "slash" or data.card:isCommonTrick()) then
      return data.from ~= player
    end
  end,
  on_cost = function(self, event, target, player, data)
    local dat = data.from.id
    if player.room:askForSkillInvoke(player, sk__lingruo.name, nil, "#sk__lingruo-invoke::" .. dat) then
      event:setCostData(self, { tos = { dat } })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self).tos[1])
    room:doIndicate(player.id, { to.id })
    local info1, info2 = {}, {}
    for _, id in ipairs(player:getCardIds("he")) do
      local type = Fk:getCardById(id):getTypeString()
      info1[type] = (info1[type] or 0) + 1
    end
    for _, id in ipairs(to:getCardIds("he")) do
      local type = Fk:getCardById(id):getTypeString()
      info2[type] = (info2[type] or 0) + 1
    end
    local n = 0
    for _, type in ipairs({ "basic", "trick", "equip" }) do
      info1[type] = info1[type] or 0
      info2[type] = info2[type] or 0
      if info1[type] > info2[type] then
        n = n + 1
      end
    end
    if n == 0 then return end
    for i = 1, n, 1 do
      local choices = {}
      if not player.dead then
        table.insert(choices, 1)
      end
      if not player.dead and not to.dead and not to:isNude() then
        table.insert(choices, 2)
      end
      if not to.dead and not to:isNude() then
        table.insert(choices, 3)
      end
      if #choices == 0 then return end
      local num = table.random(choices)
      if num == 1 then
        player:drawCards(1, sk__lingruo.name)
      else
        local card = table.random(to:getCardIds("he"))
        if num == 2 then
          room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, sk__lingruo.name, nil, false, player)
        else
          room:throwCard(card, sk__lingruo.name, to, to)
        end
      end
    end
  end,
})

return sk__lingruo
